﻿using SuperMoney.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperMoney.Datas
{
	/// <summary>
	/// 商品数据服务。
	/// </summary>
	public class CommodityDbService
	{
		/// <summary>
		/// 商品数据服务实例。
		/// </summary>
		public static readonly CommodityDbService Instance = new CommodityDbService();

		/// <summary>
		/// 
		/// </summary>
		/// <param name="commodity"></param>
		public void Create(Commodity commodity)
		{
			commodity.AddTime = DateTime.Now;
			commodity.UpdateTime = DateTime.Now;
			using (var db = MRMDB.New())
			{
				db.CommoditySet.Add(commodity);
				db.SaveChanges();
			}
		}

		/// <summary>
		/// 获取某个商品。
		/// </summary>
		/// <param name="id">商品编号。</param>
		/// <returns></returns>
		public Commodity Get(long id)
		{
			using (var db = MRMDB.New())
			{
				return db.CommoditySet.Where(p => p.Id == id).SingleOrDefault();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="count"></param>
		/// <param name="page"></param>
		/// <returns></returns>
		public List<Commodity> GetList(int count, int page)
		{
			using (var db = MRMDB.New())
			{
				int skip = (page - 1) * count;
				return db.CommoditySet.OrderBy(p=>p.Id).Skip(skip).Take(count).ToList();
			}
		}
	}
}
